<?php
class sfBreadcrumbs {
	
	protected static $instance;
	
	private $current_action;
	
	private $current_section;
	
	private $current_module;
	
	private function __consruct() {}
	
	/**
	* Array that holds all sfBreadcrumbModule objects of the current app
	*
	* @var array
	*/
	private $modules = array();
	
	/**
	* Singleton function
	*
	* @return sfBreadcrumbs the singleton instance
	*/
	public static function getInstance() {
		
		if (!isset(self::$instance)) {
			
			$class = __CLASS__;
			
			self::$instance = new $class();
			
			self::$instance->modules = sfBreadcrumbYamlHandler::init();
			
		}
		
		return self::$instance;
		
	}
	
	/**
	* Saves the current sfBreadcrumbs to breadcrumbs.yml
	*
	*/
	public function dump() {
		
		sfBreadcrumbYamlHandler::dump($this->modules);
		
	}
	
	/**
	* Returns the current modules
	*
	* @return array sfBreadcrumbModule array
	*/
	public function getModules() {
		
		return $this->modules;
		
	}
	
	
	public function setModules(array $modules) {

		$this->modules = $modules;

	}
	
	/**
	* Returns sfBreadcrumbModule with a specified symfony name
	*
	* @param string symfony name
	* @return sfBreadcrumbModule the required module
	*/
	public function getModule($name) {
		
		foreach( $this->modules as $module ) {
			
			if ( $module->getSymfonyName() == $name ) {
				
				return $module;
				
			}
			
		}
		
	}
	
	public function getCurrentAction() {
		
		return $this->current_action;
		
	}
	
	public function setCurrentAction( $current_action ) {
		
		$this->current_action = $current_action;
		
	}
	
	public function getCurrentSection() {
		
		return $this->current_section;
		
	}
	
	public function setCurrentSection( $current_section ) {
		
		$this->current_section = $current_section;
		
	}
	
	public function getCurrentModule() {
		
		return $this->current_module;
		
	}
	
	public function setCurrentModule( $current_module ) {
		
		$this->current_module = $current_module;
		
	}
	
	/**
	* Returns a textual representaion of the current sfBreadcrumbs based on the current module, section, and action
	* specified in the breadcrumbs.yml
	*
	* @param string separator used to separate breadcrumbs items. default is &raquo;
	* @return string textual representaion of the current sfBreadcrumbs
	*/
	public function getBreadcrumbText( $separator = '&raquo;', $show_links = true) {
		
		sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18N'));
		
		// get last action stack entry for the current request
		$action_stack_entry = sfContext::getInstance()->getActionStack()->getLastEntry();
		
		// name of the current module
		$current_module = $action_stack_entry->getModuleName();
		
		// name of the current action
		$current_action = $action_stack_entry->getActionName();
		
		// ref. to hold an instance of sfBreadcrumbModule that represents the current module
		$module = null;
		
		// textual representaion of the current sfBreadcrumbs
		$string = '';
		
		// loop over the available sfBreadcrumbModule modules to find the instance that represents the current module
		foreach($this->modules as $_module) {
			
			if( $_module->getSymfonyName() == $current_module ){
			
				$module = $_module;
				
				break;
			
			}
			
		}
		
		if ($module == null) {
			return $string;
		}
		
		// sfBreadcrumbSection instance that represents the current section as specified in breadcrumbs.yml

		$section = $module->findSection($current_action);
		
		if( $section == null ){

			// currently viewing the module/index action
			$string .= __($module->getDisplayName());
			
		} else {
			
			// sfBreadcrumbAction instance that represents the current action
			$action = $section->getAction($current_action);
			
			if( $show_links ){
		
				$string .= $module->getLink().' '.$separator.' '.$section->getLink().' '.$separator.' '.$action->getDisplayName();
		
			} else {

				$string .= __($module->getDisplayName()).' '.$separator.' '.__($section->getDisplayName()).' '.$separator.' '.__($action->getDisplayName());
			
			}
			
		}
		
		
		
		
		/*if( $action->hasParameters() ){
			
			$string .= ' '.$separator.' ';
			
			foreach($action->getParameters as $parameter) {
				
				$string .= $parameter->getDisplayName();
				
			}
		
		}*/
		
		return $string;
		
	}
	
}
